﻿using System;
using System.Collections.Generic;
using System.Data;
using IDAL.RF.Warehouse;
using Model.RF.Warehouse;

namespace BLL.RF.Warehouse
{
    public class StockOut : BaseService<StockOutInfo>
    {
        private static readonly IStockOut dal = DALFactory.DataAccess.CreateStockOut();

        public override void SetCurrentRepository()
        {
            CurrentRepository = dal;
        }

        /// <summary>
        /// 针对中兴产品逐个检查
        /// 1.是否包含50030900
        /// 2.是否有有效年月
        /// 3.是否超期
        /// </summary>
        public bool CheckOverdueProductSNs(IList<string> list, out string message)
        {
            message = "";
            foreach (string item in list)
            {
                int index = item.IndexOf("50030900");
                if (index <= 0)
                {
                    message += item + "，";
                }
            }
            if (!string.IsNullOrEmpty(message))
            {
                message = "以下序列号：" + message + "不是中兴产品，不包括供应商代码50030900，请检查！";
                return false;
            }
            else
            {
                foreach (string item in list)
                {
                    int index = item.IndexOf("50030900");
                    int year, month;
                    if (!(int.TryParse(item.Substring(index + 8, 2), out year) && int.TryParse(item.Substring(index + 10, 2), out month)))
                    {
                        message += item + "，";
                    }
                }
                if (!string.IsNullOrEmpty(message))
                {
                    message = "以下序列号：" + message + "不是中兴产品，供应商代码50030900后不是有效的年份月份，请检查！";
                    return false;
                }
                else
                {
                    foreach (string item in list)
                    {
                        int index = item.IndexOf("50030900");
                        int year, month;
                        if ((int.TryParse(item.Substring(index + 8, 2), out year) && int.TryParse(item.Substring(index + 10, 2), out month)))
                        {
                            if ((DateTime.Now.Year - 2000 - year) * 12 + DateTime.Now.Month - month > 6)
                            {
                                message += item + "，";
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(message))
                    {
                        message = "以下序列号：" + message + "超期，请检查！";
                        return false;
                    }
                    return true;
                }
            }
        }

        /// <summary>
        /// 1.检查是否重复出库
        /// 2.检查条码是否属于相应产品类型
        /// </summary>
        public bool StockOut_Insert(StockOutInfo si, DataTable dt, bool is_IgnorecheckPackage, out string message)
        {
            if (!is_IgnorecheckPackage)
            {
                string invalidpackagesns = dal.StockOut_InvalidPackageSNs(si, dt);
                if (!string.IsNullOrEmpty(invalidpackagesns))
                {
                    message = "以下条码没有包装记录：" + invalidpackagesns + "不允许出库，请检查";
                    return false;
                }
            }
            string InvalidStockOutSNs = dal.StockOut_InvalidStockOutSNs(si, dt);
            if (!string.IsNullOrEmpty(InvalidStockOutSNs))
            {
                message = "以下条码已经出库过：" + InvalidStockOutSNs + "不允许再次出库，请检查";
                return false;
            }
            else
            {
                string InvalidPIDSNs = dal.StockOut_InvalidPIDSNs(si, dt);
                if (!string.IsNullOrEmpty(InvalidPIDSNs))
                {
                    message = "以下条码不属于产品类型" + si.ProductTypeID + "：" + InvalidPIDSNs + "不允许出库，请检查";
                    return false;
                }
                message = "";
                return dal.StockOut_Insert(si, dt);
            }
        }

        public bool StockOut_DeleteBatch(StockOutInfo si, DataTable itemdt)
        {
            return dal.StockOut_DeleteBatch(si, itemdt);
        }

        public IList<StockOutInfo> StockOut_GetbyDT(DateTime dt1, DateTime dt2)
        {
            return dal.StockOut_GetbyDT(dt1, dt2);
        }

        public IList<StockOutInfo> StockOut_GetbyPIDDT(string productTypeID, DateTime dt1, DateTime dt2)
        {
            return dal.StockOut_GetbyPIDDT(productTypeID, dt1, dt2);
        }
    }
}
